<?php

/**
 * CTree.
 *
 * This file is the widget for the common Moco tree 
 * It allows to display the Tree and it is similar to the CTreeView, but allows any layout and loading with the url.
 *
 * To init the tree widget, please put the valid parameters
 *
 * @author Sysprog <sysprog@martis-ua.com>
 * @link http://www.tmx.com.ua/
 * @copyright Copyright &copy; 2011 
 * @license http://www.tmx.com.ua/license/
 * @version 1.0.0.1 $
 */
 
 class CTree extends CWidget
 {
	/**
	 * @var string identificator of the dialog
	 */
	public $id = "";
	
	/**
	 * @var array the list of used labels
	 *
	 * <ul>
	 * <li>title 	: base title of the dialog. Could be also set by $this->title</li>
	 * <li>up 		: text of the scroll up button</li>
	 * <li>down  	: text of the scroll down button</li>
	 * </ul>
	 */
	public $labels = array("up" => "Вверх", "down" => "Вниз");
	
	/**
	 * @var array data of the tree. 
	 * Template of the row : 
	 *     array( 'nodeId' => '',			- identificator of the node. Autogenerated if blank
	 *			  'nodeTitle' => '',		- title of the node
	 *			  'selected' => false,		- whether node is selected
	 *			  'isOpen' => false,		- whether node need show opened
	 *			  'nodeChildren' => array() - array of the child nodes
	 * 			  'nodeTemplate' => ''		- how to show single node
	 *			  'nodeUrl' => ''			- the url with the child nodes json. CTree::$url used by default
	 *			  'hasChildren' => false	- whether children exists for the node
	 *			)
	 */
	public $data = array();

	
	/**
	 * @var string the sliders (up and down) allows to scroll the tree
	 */
	public $showSliders = false;
	
	
	/**
	 * @var boolean whether to show dialog modal
	 */
	public $modal = true;
	

	/**
	 * @var string the url where content is loaded. Current url is used by default
	 * See {@link CHtml::normalizeUrl} for possible Url formats
	 */
	public $url = '';
	
	/**
	 * @var boolean switch on the async loading of the tree content. You should also set up $this.url parameter.
	 */
	public $ajaxLoad = false;

	/**
	 * @var integer width of the popup dialog
	 */
	public $width;

	/**
	 * @var integer height of the 
	 */
	public $height;
	
	/**
	 * @var string the client side function would be called after the tree content is refreshed $this->onRefresh. Format: function() { body }
	 */
	public $onRefresh = '';

	/**
	 * @var string the client side function would be called after the node content is loaded $this->onLoad. Format: function() { body }
	 */
	public $onLoad = '';
	
	/**
	 * @var string the client side function called after the node clicked. Format: function(item) { body }
	 */
	public $onClick = '';
	
	/**
	 * @var string the client side function called after the node opens. Format: function(item) { body }
	 */
	public $onOpen = '';
	
	/**
	 * @var string the client side function called after the node closes. Format: function(item) { body }
	 */
	public $onClose = '';
	
	/**
	 * @var boolean the tree opens all the nodes when showing
	 */
	public $openAll = false;
	
	/**
	 * @var boolean whether to show the scrollers or the up and down buttons
	 */
	public $allowScroller = false;
	
	/**
	 * @var boolean whether to select first element
	 */
	public $firstSelect = false;
	
	/**
	 * @var string how to show the overflow of the tree content 
	 */
	public $overflow = 'auto';
	
	public $multi = false;

	public $initClientScript = true;

	public $options = array();

	public function init()
	{
		$id = $this->getId();
		if($id == '')
		{
			$id = rand(5, 10);
		}

		if(!empty($this->url) || empty($this->url) && ($this->ajaxLoad == true) )
		{			
			$this->url = CHtml::normalizeUrl($this->url);
			$this->ajaxLoad = true;
		}

		if($this->width < 1)
		{
			$this->width = '100%';
		}

		if($this->height < 1)
		{
			$this->height = 400;
		}

		$options = $this->getOptions();
		$options['id'] = $id;

		$cs = Yii::app()->getClientScript();

		if($this->initClientScript)
		{
			$jsUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.components.widgets.ctree.js'), false, -1, YII_DEBUG);
			$cs->registerScriptFile($jsUrl."/tree.js");
			$cssUrl = Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias('application.components.widgets.ctree.css'), false, -1, YII_DEBUG);
			$cs->registerCssFile($cssUrl."/tree.css");
		}
		
		$onRefresh = '';
		if($this->onRefresh != '')
		{
			$onRefresh = $this->onRefresh;
			if(!preg_match('/function/', $onRefresh))
			{
				$onRefresh = "function() { ".$onRefresh." }";
			}
		}

		$onLoad = '';
		if($this->onLoad != '')
		{
			$onLoad = $this->onLoad;
			if(!preg_match('/function/', $onLoad))
			{
				$onLoad = "function() { ".$onLoad." } ";
			}
		}
		
		$onClick = '';
		if($this->onClick != '')
		{
			$onClick = $this->onClick;
			if(!preg_match('/function/', $onClick))
			{
				$onClick = 'function (item) { '.$onClick.' } ';
			}
		}
		
		$onOpen = '';
		if($this->onOpen != '')
		{
			$onOpen = $this->onOpen;
			if(!preg_match('/function/', $onOpen))
			{
				$onOpen = ' function (item) { '.$onOpen.' }';
			}
		}

		$onClose = '';
		if($this->onClose != '')
		{
			$onClose = $this->onClose;
			if(!preg_match('/function/', $onClose))
			{
				$onClose = 'function (item) { '.$onClose." } ";
			}
		}

		$script = ' $.moco.trees.add(null, 
					{ id : "tree-'.$id.'", sufix: "'.$id.'", multi : "'.$options['multi'].'", url : "'.$options['url'].'", width : "'.$options['width'].'px; ", height: "'.$options['height'].'px", openAll : '.(isset($options["openAll"]) ? $options["openAll"] : "false" ).', ajaxLoad : '.(isset($options["ajaxLoad"]) ? $options["ajaxLoad"] : "false" ).', firstSelect : '.(isset($options["firstSelect"]) ? $options["firstSelect"] : "false" ).'  }); ';

		if(strlen($onRefresh) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].onRefresh = '.$onRefresh.'; ';
		}

		if(strlen($onLoad) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].onLoad= '.$onLoad.'; ';
		}

		if(strlen($onClick) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].onClick= '.$onClick.'; ';
		}

		if(strlen($onOpen) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].onOpen= '.$onOpen.'; ';
		}

		if(strlen($onClose) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].onClose= '.$onClose.'; ';
		}

		if(count($this->data) > 0)
		{
			$script .= ' $.moco.trees["tree-'.$id.'"].data = '.json_encode($this->data).'; ';
		}

		$script .= ' $.moco.trees["tree-'.$id.'"].init(); ';

		$cs->registerScript('Moco.Tree#'.$id, $script);

		$this->render('tree', array('options' => $options));
	}

	public function getOptions()
	{
		$options = $this->options;

		foreach(array('labels', 'url', 'ajaxLoad', 'width', 'height', 'overflow', 'onRefresh', 'multi', 'onOpen', 'onClose', 'onLoad', 'onClick', 'openAll', 'firstSelect') as $name)
		{
			if($this->$name != null)
			{
				$options[$name] = $this->$name;
			}
			else
			{
				$options[$name] = null;
			}
		}

		return $options;
	}

	public function run()
	{
	}
 }
 
?>